/*
** ############################################################################
**     Project   : osdee - Operative Systems Design for Embedded Envrionments
**     File      : pit.h
**     Revision  ; 1.0
**     Abstract  :
**         Interface for PIT control functions.
**     Processor : MC9S12XEP100CVL
**     Version   : Component 01.042, Driver 01.05, CPU db: 3.00.036
**     Datasheet : MC9S12XEP100 Rev. 1.19 12/2008
**
**     Copyright : 2014 Fernando Rodriguez, Open source with out any responsability.
**     
**     mail      : frr@gmail.com
** ############################################################################
*/

#ifndef _PIT_H_
#define _PIT_H_

#include "MCU_Cfg.h"
#include <MC9S12XEP100.h>
#include "cdef.h"
#include "pll.h"
#include "os_isr.h"

/*
** ===================================================================
**     Configuration of Micro Timers
**          Basic constants definition. 
** ===================================================================
*/
#define    PIT_SUPPORTED_CHANNELS        8  /* Number of supported PIT channels */

//#define ISR(isr_name) void Os_Entry_##isr_name(void)

/*
** ===================================================================
**     Type        :  tePIT_Channel_Id
**
**     Description :
**         Enumeration of supported PIT Channels.
** ===================================================================
*/
typedef enum
{
  PIT0 = 0,
  PIT1 = 1,
  PIT2 = 2,
  PIT3 = 3,
  PIT4 = 4,
  PIT5 = 5,
  PIT6 = 6,
  PIT7 = 7
}tePIT_Channel_Id;

/*
** ===================================================================
**     Type        :  tePIT_Type
**
**     Description :
**         Enumeration of PIT Channel types (Configurable).
**         PIT_Type_HihgResolution:    Values: Min=2us, Max=1,365us. 
**                                     Purpose: High accuarancy in micro seconds (23us, 512us, 781us) 
**         PIT_Type_HihgPeriod:        Values: Min=2us, Max=136,530us. 
**                                     Purpose: High periods support (1000us, 5100us, 110000us)
** ===================================================================
*/
typedef enum
{
 PIT_Type_HihgResolution = 0,     /* Values: Min=2us, Max=1,365us.*/ 
 PIT_Type_HihgPeriod              /* Values: Min=2us, Max=136,530us.*/
}tePIT_Type;

/*
** ===================================================================
**     Type        :  tsPIT_Channel
**
**     Description :
**         Atomic configuration for each PIT Channel.
**         - tChannel_Id : Channel Id.
**         - u32Period: Timmer period in micro seconds.
**         - tType    : Channel type (PIT_Type_HihgResolution or PIT_Type_HihgPeriod).
**         - tCallback: Function to be called when Timmer elapses. 
** ===================================================================
*/
typedef struct
{
 tePIT_Channel_Id tChannel_Id; 
 UINT32 u32Period;
 tePIT_Type tType;
 tCallbackFunction tCallback;
}tsPIT_Channel;
	
/*
** ===================================================================
**     Configuration of Micro Timers
**          These values are not intended to be modified by the client applications. 
** ===================================================================
*/
#define    CFG_MICROTIMER0_COUNT            1     /* For high resolution Timers Min=2us, Max = 1,365us*/
#define    PIT_HIGHRES_MIN_VALUE            2     /* High Resolution timmer minimum value*/
#define    PIT_HIGHRES_MAX_VALUE            1365  /* High Resolution timmer maximum value*/

#define    CFG_MICROTIMER1_COUNT            100   /* For high period timers Min=2us, Max = 136,530us*/
#define    PIT_HIGHPERIOD_MIN_VALUE         2     /* High Period timmer minimum value*/
#define    PIT_HIGHPERIOD_MAX_VALUE         136530/* High Period timmer maximum value*/

/*
** ===================================================================
**     Constants for PIT Counters. 
** ===================================================================
*/
#define    PIT_MICROTIMER0_DIV              CFG_MICROTIMER0_COUNT - 1
#define    PIT_MICROTIMER1_DIV              CFG_MICROTIMER1_COUNT - 1  
#define    PIT_TIMER_FACTOR                 (UINT16)(mcal_mcu_u32BusFrequency / 1000000)


/*
** ===================================================================
**     Method      :  Mcu_vPTI_Initialize(void)
**
**     Description :
**         Global PIT Module initialization.
** ===================================================================
*/
void Mcu_vPIT_General_Initialize(void);

/*
** ===================================================================
**     Method      :  Mcu_vPTI_Initialize(tsPIT_Channel tChannel)
**
**     Description :
**         PIT Channel initialization.
** ===================================================================
*/
void Mcu_vPTI_Initialize(tsPIT_Channel* tChannel);

/*
** ===================================================================
**     Method      :  Mcu_vPTI_Start(tsPIT_Channel tChannel)
**
**     Description :
**         Starts PIT Channel countdown.
** ===================================================================
*/
void Mcu_vPTI_Start(tsPIT_Channel* tChannel);

/*
** ===================================================================
**     Method      :  Mcu_vPTI_Stop(tsPIT_Channel tChannel)
**
**     Description :
**         Stops PIT Channel countdown.
** ===================================================================
*/
void Mcu_vPTI_Stop(tsPIT_Channel* tChannel);

/*
** ===================================================================
**     Method      :  Mcu_vPTI_Enable(tsPIT_Channel tChannel)
**
**     Description :
**         Enables PIT Interruptions handling
** ===================================================================
*/
void Mcu_vPTI_Enable(tsPIT_Channel* tChannel);

/*
** ===================================================================
**     Method      :  Mcu_vPTI_Disable(tsPIT_Channel tChannel)
**
**     Description :
**         Disables PIT Interruptions handling.
** ===================================================================
*/
void Mcu_vPTI_Disable(tsPIT_Channel* tChannel);

/*
** ===================================================================
**     Method      :  Mcu_vPIT_ChannelX_Isr(void)
**
**     Description :
**         PIT channel (0-7) ISR function definitions. 
**         All interrupts handled in Main CPU, no XGATE enabled.
** ===================================================================
*/
#pragma CODE_SEG __NEAR_SEG NON_BANKED
	void OS_Entry_Mcu_vPIT_Channel0_Isr(void);
	void Mcu_vSWI_Isr(void);
	void Mcu_vPIT_Channel1_Isr(void);
	void Mcu_vPIT_Channel2_Isr(void);
	void Mcu_vPIT_Channel3_Isr(void);
	void Mcu_vPIT_Channel4_Isr(void);
	void Mcu_vPIT_Channel5_Isr(void);
	void Mcu_vPIT_Channel6_Isr(void);
	void Mcu_vPIT_Channel7_Isr(void);
#pragma CODE_SEG DEFAULT

/*
** ===================================================================
**     Method      :  Mcu_vHandle_PIT_Interrupt(tePIT_Channel_Id tChannel_Id)
**
**     Description :
**         Wrapps PIT Channel interruption handling. 
**         Clears interruption flag, and calls specified Callback.
** ===================================================================
*/
void Mcu_vHandle_PIT_Interrupt(tePIT_Channel_Id tChannel_Id);

#endif /* _PIT_H_ */